import { formatTime } from '@/utils/utils.js'
import lawBankTopBar from 'components/lawBankTopBar/lawBankTopBar'
import BiaofunRegionTwoLevelAll from "@/components/biaofun-region/biaofun-region-two-level-all.vue"
import { getPolicyDataList, getTaxList } from '@/api/lawBank'//js
export default {
	components: {
		lawBankTopBar,
		BiaofunRegionTwoLevelAll
	},
	data() {
		return {
			title: '',
			type: null,
			backgroundUrl: require('@/static/img/lawBank/topBar_bg_01.png'),
			potencyList: [{
				id: "现行有效",
			},
			{
				id: "部分失效/废止",
			},
			{
				id: "全文失效/废止",
			},
			{
				id: "已修订",
			}],
			scrollTop: 0,
			form: {
				keyword: '',//标题关键字
				cons_keyword: '',//全文关键字
				zh_keyword: '',//国家税务总局公告
				zh_year: '',//发文字号 年份
				zh_no: '',//发文字号 号
				city_code: '',//城市编码
				policy_cate_id: '',//分类id
				startDate: '',//开始发文日期
				endDate: '',//结束发文日期
				xiaoli_id: '',//效力注释
			},
			query: {
				page: 1,
				limit: 1,
				type: '',
			},//搜索参数
			shengshiquTxt: {
				name: '',
			},//省市区
			taxTypeIndex: -1,//税种下标
			taxTypeList: [],//税种
			showDateTime: false,//时间弹窗显示隐藏
			dateType: -1,////开始结束时间弹窗区分下标
			value1: Number(new Date()),
			count: 0,
			flag: false,//是否显示重置按钮
			isShow: false,
		};
	},
	methods: {
		//选择地区
		selectRegion(res) {
			this.shengshiquTxt = res[1];
			this.form.city_code = res[1].id;
		},
		// 取消选择
		regionsCancel() {
			this.shengshiquTxt = '';
			this.form.city_code = '';
		},
		//选择税种
		categoryChange(res) {
			console.log(res);
			this.taxTypeIndex = res.detail.value;
			this.form.policy_cate_id = this.taxTypeList[this.taxTypeIndex].id;
		},
		// 取消选择税种
		categoryCancel() {
			this.taxTypeIndex = 0;
			this.form.policy_cate_id = '';
		},
		// 选择效力
		potencyChange(res) {
			this.form.xiaoli_id = this.potencyList[res.detail.value].id;
		},
		// 取消选择效力
		potencyCancel() {
			this.form.xiaoli_id = '';
		},
		// 打开时间picker
		openDatetimePicker(n) {
			this.dateType = n
			this.showDateTime = true;
		},

		// 关闭时间picker
		cancelDateTime() {
			this.showDateTime = false;
		},
		//时间选择器确定
		confirmDateTime(e) {
			let date = formatTime(e.value);
			this.form[this.dateType === 1 ? 'startDate' : 'endDate'] = date;
			this.getData()
			this.showDateTime = false;
		},
		// 时间格式化年月日
		formatter(type, value) {
			if (type === 'year') {
				return `${value}年`
			}
			if (type === 'month') {
				return `${value}月`
			}
			if (type === 'day') {
				return `${value}日`
			}
			return value
		},
		//判断是否有输入数据
		isFlag() {
			let flag = Object.values(this.form).every(value => value == '');
			console.log(flag);
			this.flag = !flag
		},
		//重置
		reset() {
			this.form = {
				keyword: '',//标题关键字
				cons_keyword: '',//全文关键字
				zh_keyword: '',//国家税务总局公告
				zh_year: '',//发文字号 年份
				zh_no: '',//发文字号 号
				city_code: '',//城市编码
				policy_cate_id: '',//分类id
				startDate: '',//开始发文日期
				endDate: '',//结束发文日期
				xiaoli_id: '',//效力注释
			};
			this.shengshiquTxt = {
				name: ''
			};
			this.taxTypeIndex = -1;//
			this.dateType = -1;//开始结束时间弹窗区分下标
			this.count = 0;
		},

		//查一下
		goData() {
			if (this.flag) {
				uni.setStorageSync('searchQuery', this.form)
				if (this.type == 1) {
					uni.navigateTo({
						url: `../revenue/index?isSearch=true`,
					})
				} else {
					uni.navigateTo({
						url: `../favorable/index?isSearch=true`,
					})
				}
			} else {
				this.$used.msg('请输入搜索内容');
			}
		},



		//请求数据
		getData() {
			this.isFlag()
			let data = {
				...this.form,
				...this.query
			}
			getPolicyDataList(data).then(res => {
				if (res.data.length > 0) {
					this.count = res.data[0].all_count;
				} else {
					this.count = 0;
				}
			})
		},
		//获取税种
		getTaxType() {
			getTaxList({}).then((res) => {
				if (res.code == 1) {
					this.taxTypeList = res.data;
				} else {
					this.$tools.showToast(res.msg);
				}
			})
				.catch((err) => {
					console.log('错误回调', err);
				});
		},

	},
	onReady() {
		this.$refs.datetimePicker.setFormatter(this.formatter)
	},
	onLoad(e) {
		let { type } = e
		this.title = type == 1 ? '税收法规库' : '优惠政策'
		this.query.type = type == 1 ? 2 : 1
		this.type = type;
		this.getTaxType()
	},

}